package com.hymall.service.wx;

import com.hymall.bean.*;
import com.hymall.bean.vo.WxSearchKeywordVO;
import com.hymall.mapper.MarketKeywordMapper;
import com.hymall.mapper.MarketSearchHistoryMapper;
import com.hymall.mapper.MarketUserMapper;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 长平居士 - rolfcui@outlook.com
 * @description 搜索模块 实现类
 * @date 2022/7/19 17:31
 */
@Service
public class SearchServiceImpl implements SearchService {

    @Autowired
    MarketUserMapper marketUserMapper;
    @Autowired
    MarketKeywordMapper marketKeywordMapper;
    @Autowired
    MarketSearchHistoryMapper marketSearchHistoryMapper;

    @Override
    public WxSearchKeywordVO index() {
        WxSearchKeywordVO vo = new WxSearchKeywordVO();
        MarketKeywordExample example = new MarketKeywordExample();
        List<MarketKeyword> list = marketKeywordMapper.selectByExample(example);
        MarketKeywordExample.Criteria criteria = example.createCriteria();
        criteria.andIsDefaultEqualTo(true);

        vo.setDefaultKeyword(marketKeywordMapper.selectByExample(example).get(0));
        vo.setHotKeywordList(list.stream()
                .filter(item -> item.getIsHot())
                .collect(Collectors.toList()));

        // 获取用户 ID
        Subject subject = SecurityUtils.getSubject();
        if (subject.getPrincipals() == null) {
            return null;
        }
        MarketUser principal = (MarketUser) subject.getPrincipals().getPrimaryPrincipal();
        MarketUserExample userExample = new MarketUserExample();
        MarketUserExample.Criteria userCriteria = userExample.createCriteria();
        userCriteria.andUsernameEqualTo(principal.getUsername());
        Integer userID = marketUserMapper.selectByExample(userExample).get(0).getId();

        MarketSearchHistoryExample shExample = new MarketSearchHistoryExample();
        MarketSearchHistoryExample.Criteria shCriteria = shExample.createCriteria();
        shCriteria.andUserIdEqualTo(userID);
        List<MarketSearchHistory> shList = marketSearchHistoryMapper.selectByExample(shExample);
        vo.setHistoryKeywordList(shList);

        return vo;
    }

    @Override
    public List<String> getKeyword(String keyword) {
        MarketKeywordExample example = new MarketKeywordExample();
        MarketKeywordExample.Criteria criteria = example.createCriteria();
        if (keyword == null || keyword == "") {
            return null;
        }
        criteria.andKeywordLike("%" + keyword + "%");
        List<MarketKeyword> classList = marketKeywordMapper.selectByExample(example);
        List<String> strList = classList.stream()
                .map(item -> item.getKeyword())
                .collect(Collectors.toList());
        return strList;
    }

    @Override
    public void clearHistory() {
        MarketSearchHistoryExample example = new MarketSearchHistoryExample();
        marketSearchHistoryMapper.deleteByExample(example);
    }
}
